package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.grandchild.SchoolSiteData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class SchoolSiteDaoImpl extends TempDataObjectDao<SchoolSiteData> {

    public static String getSchoolSiteDataSql = "select * from `t_u_school_site` where `userId`=?";
    public Map<Integer, List<SchoolSiteData>> getSchoolSiteData(long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, List<SchoolSiteData>> dataMap = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getSchoolSiteDataSql);
            int i = 1;
            call.setLong(i++, userId);
            rs = call.executeQuery();
            while (rs.next()) {
                SchoolSiteData data = parse(rs);
                int schoolType = data.getSchoolType();
                if (!dataMap.containsKey(schoolType)) {
                    dataMap.put(schoolType, new ArrayList<>());
                }
                List<SchoolSiteData> siteDataList = dataMap.get(schoolType);
                siteDataList.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return dataMap;
    }

    public static String updateSchoolSiteDataSql = "update `t_u_school_site` set childId=? where userId=? and schoolType=? and pos=?";
    @Override
    protected boolean updateData(SchoolSiteData schoolSiteData) {
        schoolSiteData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.VARCHAR, schoolSiteData.getChildId()));
        para.put(i++, new DbParameter(Types.BIGINT, schoolSiteData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, schoolSiteData.getSchoolType()));
        para.put(i++, new DbParameter(Types.INTEGER, schoolSiteData.getPos()));
        if (getDb().executeNoneQuery(updateSchoolSiteDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertSchoolSiteDataSql = "insert into `t_u_school_site`(userId,schoolType,pos,childId) values (?,?,?,?)";
    @Override
    protected boolean addData(SchoolSiteData schoolSiteData) {
        schoolSiteData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, schoolSiteData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, schoolSiteData.getSchoolType()));
        para.put(i++, new DbParameter(Types.INTEGER, schoolSiteData.getPos()));
        para.put(i++, new DbParameter(Types.VARCHAR, schoolSiteData.getChildId()));
        if (getDb().executeNoneQuery(insertSchoolSiteDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected SchoolSiteData parseData(ResultSet rs) throws SQLException {
        SchoolSiteData schoolSiteData = new SchoolSiteData();
        schoolSiteData.setUserId(rs.getLong("userId"));
        schoolSiteData.setSchoolType(rs.getInt("schoolType"));
        schoolSiteData.setPos(rs.getInt("pos"));
        schoolSiteData.setChildId(rs.getString("childId"));

        return schoolSiteData;
    }
}
